package de.tum.in.eist.poll.server;



import java.util.NoSuchElementException;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import de.tum.in.eist.poll.pub.service.PushService;
import de.tum.in.eist.poll.pub.shared.LecturePush;
import de.tum.in.eist.poll.pub.shared.TimeoutException;
import de.tum.in.eist.poll.pub.shared.User;

public class PushServiceImpl extends RemoteServiceServlet implements
		PushService {
	public static Object lock = new Object();
	public static LecturePush lecture = new LecturePush();

	@Override
	public LecturePush getNotification(User user, int lastId) throws TimeoutException {
		synchronized (lock) {
			try {
				lock.wait(50000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(/*lecture.getLecture().containsStudent((Student)user) && */lecture.getId() > lastId && !lecture.getUser().getName().equals(user.getName())){
				return lecture;
			}
			else{
				throw(new TimeoutException());
			}
		}
	}

	@Override
	public int getLastID() throws NoSuchElementException {
		if(lecture.getLecture() != null){
			return lecture.getId();
		}
		else{
			throw new NoSuchElementException();
		}
	}
}
